package com.liuguangzhou.controller;

import com.liuguangzhou.model.Employee;
import com.liuguangzhou.model.Salary;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName EmployeeContrller
 * @Description: TODO
 * @Author liuguangzhou
 * @Date 2020/3/14
 * @Version V1.0
 **/
@Controller
public class EmployeeContrller {

//    @RequestMapping(value = "employee",method = RequestMethod.GET )
    @GetMapping("employee") //跟上面一行作用一样
    @ResponseBody
    public List<Employee> get(){
        List list=new ArrayList();
        list.add(new Employee(1L,"samuel"));
        list.add(new Employee(2L,"samuel2"));
        return list;
    }


    /**
    *获取某个员工的信息(路径传参)
    * 1.确定资源 /employees/ {id} 使用路径占位符
    * 2.确定请求方式 GET
    * 3.确定返回结果(类型,头信息,状态码) 员工对象，content -type-application/json, 201
    +如果@PathVariable注解没有设置value，默认就是去路径上找相同名称的参数
    */


    @GetMapping("employee/{id}")
    @ResponseBody
    //不加PathVariable 不是从路径上拿 是之前那种?才能拿的到  注意不加()会拿参数的名字去找
    public Employee getById(@PathVariable("id") Long id){
        return new Employee(id,"samuel");
    }


    /**
     *删除某个员工的信息(路径传参)
     * 1.确定资源 /employees/ (name} 使用路径占位符
     * 2.确定请求方式 Delete
     * 3.确定返回结果  空文档  204
     */

    @DeleteMapping("employee/{id}")
    @ResponseBody
    public void deleteById(@PathVariable("id") Long id, HttpServletResponse httpServletResponse){
        System.out.println("删除id为:"+id+"的员工");
        httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }


    @PutMapping("employee/{id}")
    @ResponseBody
    public Employee updateById(Employee employee){
        System.out.println("put请求测试");
        return employee;
    }



    /**
     *获取某个员工某个月的薪资记录
     * 1.确定资源/ employees/ {employeeId}/salaries/ (month}
     * 2.确定请求方式
     * GET
     * 3.确定返回结果(类型，头信息,状态码)薪资对象， content -type-application/json, 200
     * @DateTimeFormat 前台传日期参数到后台接收时使用的注解
     * @JsonFormat 后台返回json数据给前台时使用的注解〉
    */
    @GetMapping("employee/{employeeid}/salary/{month}")
    @ResponseBody
    public Salary getSalary(@PathVariable Long employeeid, @PathVariable @DateTimeFormat(pattern = "yyyy-MM") Date month){
        return new Salary(1L,employeeid, BigDecimal.TEN,month);
    }

    /**
     * 给某个员工添加一条薪资记录
     * 1.确定资源/ employees/ {employeeId}/salaries
     * 2.确定请求方式
     * POST
     * 3.确定返回结果(类型，头信息,状态码)薪资对象， content-type-application/json, 201
    */
    @PostMapping("employee/{employeeid}/salary")
    @ResponseBody
    public Salary addSalary(Salary salary){ //post提交时 跟实体类的属性一样可以直接存进去
        return salary;
    }
}
